package com.example.pharmacy.controller;

import com.example.pharmacy.model.Category;
import com.example.pharmacy.model.CategoryResult;
import com.example.pharmacy.model.Result;
import com.example.pharmacy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-21
 * Time: 19:46
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/selectAllCategory")
    public Result selectAllCategory() {
        List<Category> categories = categoryService.selectAllCategory();
        if (categories == null) {
            return Result.fail("查询种类失败");
        }
        return Result.success(categories);
    }
    @RequestMapping("/addCategory")
    public Result addCategory(Category category) {
        if (category == null || !StringUtils.hasLength(category.getClassName())) {
            return Result.fail("添加种类参数有误");
        }
        Integer ret = categoryService.insertCategory(category);
        if (ret == 0) {
            Result.fail("添加种类失败");
        }
        return Result.success("");
    }
    @RequestMapping("/updateCategory")
    public Result updateCategory(Category category) {
        if (category == null || !StringUtils.hasLength(category.getClassName())) {
            return Result.fail("更新种类参数有误");
        }
        Integer ret = categoryService.updateCategory(category);
        if (ret == 0) {
            Result.fail("更新种类失败");
        }
        return Result.success("");
    }
}
